package net.huashitong.ssydt.provider.exam.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.huashitong.ssydt.exam.entity.SsExamQuestion;
import net.huashitong.ssydt.exam.entity.vo.ExamQuestion;
import net.huashitong.ssydt.exam.entity.vo.ExamQuestionQueryInVO;
import net.huashitong.ssydt.provider.exam.service.SsExamQuestionService;
import net.huashitong.ssydt.provider.web.model.PaginationResult;
import net.huashitong.ssydt.security.utils.Result;
import net.huashitong.ssydt.security.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 选择题题库接口服务
 *
 * @author hemj
 * @version 2017/8/25
 */
@Api(value = "/exam/question", tags = {"选择题题库管理（后台）"})
@RestController
@RequestMapping("/exam/question")
public class SsExamQuestionController {

    private SsExamQuestionService ssExamQuestionService;

    @Autowired
    public SsExamQuestionController(SsExamQuestionService ssExamQuestionService) {
        this.ssExamQuestionService = ssExamQuestionService;
    }

    @ApiOperation(value = "题目列表分页查询", notes = "题目列表分页查询")
    @GetMapping
    public Result<PaginationResult<SsExamQuestion>> paginationQuery(@ModelAttribute ExamQuestionQueryInVO examQuestionQueryInVO) {
        return ResultUtils.getSuccessResults(ssExamQuestionService.paginationQuery(examQuestionQueryInVO));
    }

    @ApiOperation(value = "加载题目信息", notes = "加载题目信息(包含答案选项列表)")
    @GetMapping("/{questionId}")
    public Result<ExamQuestion> get(@PathVariable Long questionId) {
        return ResultUtils.getSuccessResults(ssExamQuestionService.getQuestionInfo(questionId));
    }

    @ApiOperation(value = "新增题目", notes = "新增题目")
    @PostMapping
    public Result<Long> post(@Valid @RequestBody ExamQuestion examQuestion) {
        return ResultUtils.getSuccessResults(ssExamQuestionService.addQuestionInfo(examQuestion));
    }

    @ApiOperation(value = "修改题目", notes = "修改题目")
    @PutMapping("/{questionId}")
    public Result<String> put(@PathVariable Long questionId, @Valid @RequestBody ExamQuestion examQuestion) {
        examQuestion.setId(questionId);
        ssExamQuestionService.updateQuestionInfo(examQuestion);
        return ResultUtils.getSuccessResults();
    }


    @ApiOperation(value = "逻辑删除", notes = "逻辑删除")
    @DeleteMapping("/{questionId}")
    public Result<String> delete(@PathVariable Long questionId) {
        ssExamQuestionService.logicDelete(questionId);
        return ResultUtils.getSuccessResults();
    }
}
